Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
How store address of an array[] in a variable

This seems like a silly question. I have an array of chars and want to store the address of the array in another variable, but can't seem to declare the correct type for the array address (I'm using gcc):


IN:

int main(void){
  char cha[] = "abcde";
  char **arrayAddress = &cha;
}

OUT:

arrayaddress.c: In function ‘main’:
arrayaddress.c:3:25: warning: initialization of ‘char **’ from incompatible pointer type ‘char (*)[6]’ [-Wincompatible-pointer-types]
    3 |   char **arrayAddress = &cha;
      |                         ^

This is expected, I have read elsewhere that the type of cha should be char(*)[6]. But when I try to declare arrayAddress with this type, my program fails:


IN:

int main(void){
  char cha[] = "abcde";
  char (*)[6]arrayAddress = &cha;
}

OUT:

arrayaddress.c: In function ‘main’:
arrayaddress.c:3:10: error: expected identifier or ‘(’ before ‘)’ token
    3 |   char (*)[6]arrayAddress = &cha;
      |          ^
make: *** [<builtin>: arrayaddress] Error 1
       ^

How do I define arrayAddress correctly?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It is written:

char (*arrayAddress)[6] = &cha;

Notice that the name of variable gets tucked in middle of the expression.

over 4 years ago · Santiago Trujillo Relatório

0

Arrays decay to pointers.

  char cha[] = "abcde";

  char *p1 = cha;

  char (*arrayptr)[sizeof(cha)] = &cha;

cha, &cha[0] and &cha reference the same first element of the array cha, the only difference is the type.

  1. cha and &cha[0] has type: pointer to char
  2. &cha has type: pointer to array of 6 char elements.
over 4 years ago · Santiago Trujillo Relatório

0

If your compiler supports typeof extension (gcc does) then you can define the pointer as:

typeof(char (*)[6]) arrayAddress = &cha;

Or even cleaner as:

typeof(char[6]) * arrayAddress = &cha;
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda